package com.lysj.bhp.borrowing.controller;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingRate;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.domain.WithDraw;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/17--11:46
 * @Description:借贷记录申请审批
 */
@RestController
@RequestMapping("admin/borrowing_apply")
public class BorrowingApplyController extends BorrowingContent {

    @RequestMapping("/list")
    public Resp list() {
        Integer[] typeIds = new Integer[2];
        typeIds[0] = 1;
        typeIds[1] = 2;
        List<BorrowingRecord> borrowingRecordList = borrowingRecordRepository.findByStatusInOrderByApplyDateDesc(typeIds);
        return new Resp(borrowingRecordList);
    }

    @PostMapping("/edit")
    public Resp edit(BorrowingRecord model) {
        if(ParamUtil.isBlack(model.getDeadline())){
            return new Resp(Resp.PARAM_ERROR,"请输入期限!");
        }
        if(ParamUtil.isBlack(model.getInterest())){
            return new Resp(Resp.PARAM_ERROR,"请输入年利率!");
        }
        if(ParamUtil.isBlack(model.getRefundWay())){
            return new Resp(Resp.PARAM_ERROR,"请输入还款方式!");
        }
        if(ParamUtil.isBlack(model.getRateType())){
            return new Resp(Resp.PARAM_ERROR,"请输入利率类型!");
        }
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(model.getBorrowingRecordId());
        if(borrowingRecord == null){
            return new Resp(Resp.PARAM_ERROR,"该记录不存在!");
        }
        borrowingRecord.setDeadline(model.getDeadline());
        borrowingRecord.setInterest(model.getInterest());
        borrowingRecord.setRefundWay(model.getRefundWay());
        borrowingRecord.setRateType(model.getRateType());
        borrowingRecordRepository.save(borrowingRecord);
        return new Resp("修改成功!");
    }


    @PostMapping("/approval")
    public Resp approval(BorrowingRecord model) {
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findByBorrowingRecordIdAndStatus(model.getBorrowingRecordId(), 1);
        if (borrowingRecord == null) {
            return new Resp(Resp.PARAM_ERROR, "该申请信息已被修改!");
        }
        borrowingRecord.setStatus(model.getStatus());
        borrowingRecordRepository.save(borrowingRecord);
        return new Resp("修改成功!");
    }

    @PostMapping("/make_loans")
    public Resp makeLoans(BorrowingRecord model) throws Exception {
        if (ParamUtil.isBlack(model.getBorrowingRecordId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择借款记录");
        }
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findByBorrowingRecordIdAndStatus(model.getBorrowingRecordId(), 2);
        if (null == borrowingRecord) {
            return new Resp(Resp.PARAM_ERROR, "查无此记录,请联系管理员");
        }
        if (model.getStatus() == 3) {
            return loanMoneyService.loadMoney(borrowingRecord);
        } else {
            borrowingRecord.setStatus(model.getStatus());
            borrowingRecordRepository.save(borrowingRecord);
            return new Resp("取消放款操作成功!");
        }
    }


}
